<!DOCTYPE html>
<html lang="zh-cmn-Hans">

	<head>
		<meta http-equiv="Expires" content="0" />
		<meta http-equiv="Pragma" content="no-cache" />
		<meta http-equiv="Cache-control" content="no-cache" />
		<meta http-equiv="Cache" content="no-cache" />
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
		<title>组装规格书</title>
		<link rel="stylesheet" href="../layui/css/layui.css">
		<link rel="stylesheet" href="../jexcel-master/dist/css/jquery.jexcel.css" />
	</head>

	<body>
		<fieldset class="layui-elem-field site-demo-button">
			<legend><big><b>组装规格书</b></big></legend>
			<div class="layui-fluid layui-form layui-form-pane">
				<div class="layui-row layui-form-item">
					<label class="layui-form-label">扫描二维码:</label>
					<div class="layui-input-block">
						<input class="layui-input" placeholder="请扫入特定的二维码" id="qr" type="text" />
						<input class="layui-input" type="hidden" id="QRC" />
					</div>
				</div>
			</div>
		</fieldset>

		<fieldset class="layui-elem-field site-demo-button">
			<legend><big><b>这里是当前扫描的信息组合</b></big></legend>
			<div id="DQ" class="layui-fluid layui-form layui-form-pane">
				<div class="layui-col-sm4">
					<label class="layui-form-label">员工工号:</label>
					<div class="layui-input-block">
						<input class="layui-input" readonly=true placeholder="这里是员工工号" id="YGGH" type="text" />
					</div>
				</div>

				<div class="layui-col-sm4">
					<label class="layui-form-label">员工姓名:</label>
					<div class="layui-input-block">
						<input class="layui-input" readonly=true placeholder="这里是员工姓名" id="YGXM" type="text" />
					</div>
				</div>

				<div class="layui-row">
					<div class="layui-col-sm4">
						<label class="layui-form-label">当前机台:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="这里是当前机台" id="DQJT" type="text" />
						</div>
					</div>

					<div class="layui-col-sm4">
						<label class="layui-form-label">工单号:</label>
						<div class="layui-input-block">
							<input class="layui-input" readonly=true placeholder="这里是工单号" id="GDH" type="text" />
							<input class="layui-input" readonly=true id="liaohao" type="hidden" />
							<input class="layui-input" readonly=true id="hehao" type="hidden" />
						</div>
					</div>
				</div>

			</div>
		</fieldset>
		<fieldset class="layui-elem-field site-demo-button">
			<div class="layui-fluid layui-form layui-form-pane">
				<input type="hidden" id="did" />
				<div id="mytable" name="tb1">
				</div>
				<div class="layui-row layui-btn-container">
					<div class="layui-col-sm4">
						<button class="layui-btn  layui-btn-normal" id="keep">保存</button>
					</div>
				</div>

			</div>
		</fieldset>
		<div class="layui-tab layui-tab-card" lay-filter="demo">
			<fieldset class="layui-elem-field site-demo-button layui-btn-container">
				<!-- 手风琴测试区 -->
				<div class="layui-collapse" lay-accordion>
					<div class="layui-colla-item">
						<h2 class="layui-colla-title">点击查看该工单其它组装规格书</h2>
						<div class="layui-colla-content" id="wospe">
							<!-- 这里是需要打印的区域,也就是二维码区域 -->
							<div align="left" class="layui-row">
								<div id="YCQ" class="layui-row layui-btn-container"></div>
							</div>
						</div>
					</div>
				</div>
				<!-- 手风琴结尾 -->
			</fieldset>
		</div>
		<div class="layui-tab layui-tab-card" lay-filter="demo">
			<fieldset class="layui-elem-field site-demo-button layui-btn-container">
				<legend><big><b>组装规格书详情内容</b></big></legend>
				<div class="layui-fluid layui-form layui-form-pane">
					<div id="GGXQ" class="layui-row layui-btn-container"></div>
				</div>
			</fieldset>
		</div>

		<!-- 加载jQuery文件 -->
		<script src="../js/jquery.min.js"></script>
		<!-- 加载类ui文件 -->
		<script src="../layui/layui.js"></script>
		<!-- 加载socket.io文件 -->
		<script src="../js/socket.io.js"></script>
		<!-- 加载socket的脚本 -->
		<!-- 加载JExcel文件 -->
		<script src="../jexcel-master/dist/js/jquery.jexcel.js"></script>
		<!-- 写脚本 -->
		<script>
			var GD = "";
			var Hhtml = ""; //画窗口的画面,把程序写到Layer里面
			var Dhtml = ""; //画窗口的画面,把程序写到Layer里面
			var liaohao = ""; //把料号放全局,这样就大家都能用了
			var hehao = ""; //把盒序号放全局,这样就大家都能用了
			var GDH8HH = ""; //把工单号+盒序号放全局,这样就大家都能用了
			var ZuZhuangZD = {}; //用于支架弹窗
			var ZuZhuangZDArr = {}; //组装规格书字典组0
			var ZuZhuangZDArr1 = {}; //组装规格书字典组1
			var ZuZhuangZD1 = {}; //组装规格书内容的字典1
			DD = []; //表身数组
			var data2 = {}; //数据2对象

			//这里开始写jexcle
			var DuXie = [{ //隐藏1
				type: 'hidden',
				readOnly: true
			}, { //交易号
				type: 'hidden',
				readOnly: true
			}, { //工序
				type: 'text',
				readOnly: true
			}, { //工艺说明
				type: 'text',
				readOnly: true
			}, { //料盒编号
				type: 'text',
				wordWrap: true
			}, { //生产数
				type: 'number',
				wordWrap: true
			}, { //转序数
				type: 'number',
				wordWrap: true
			}, { //不良数
				type: 'number',
				wordWrap: true
			}, { //备注
				type: 'text',
				wordWrap: true
			}];
			var LieMing = [
				'隐藏1', '交易号', '工序', '工艺说明', '料盒编号', '生产数', '转序数', '不良数', '备注'
			];
			var LieKuan = [
				50, //隐藏1
				50, //交易号
				80, //工序
				260, //工艺说明
				200, //料盒编号
				80, //生产数
				80, //转序数
				80, //不良数
				100 //备注
			];

			//这里开始写入s2的对照表
			var STR2 = {
				"EM": "YGGH"
				//,"AT":"DQGD"
				//,"BP":"DQJT"
			}
			var Str2 = "0"; //取前面两个字符进行下面的判断
			var JT2 = {}; //机台的字典: 存扫描的次数 i
			var i = 0;
			var JTGD = {}; //这里是机台号对应工单号的数组

			//载入layui模块：layer弹出层,form表单,element元素组件做分页或其他
			layui.use(['jquery', 'layer', 'form', 'element'], function() {
				var $ = layui.$; //重点处
				var layer = layui.layer;
				var form = layui.form;
				var element = layui.element;
				var table = layui.table;

				//这里是画面准备完毕后执行的程式
				$(document).ready(function() {
					//如果HTML5WEB存储存在,就查询,否则就弹窗提示
					if(localStorage.scyggh) {
						$("#YGGH").val(localStorage.scyggh);
						$.ajax({
							type: 'POST',
							url: "http://172.31.3.27:84/index.php?s=/scancode/Product/xm", //172.31.3.27:84//127.0.0.1:85,//POST傳一個超長字串到後端
							data: "qrc=" + localStorage.scyggh,
							dataType: "json",
							success: function(sss) {
								if(sss.msg) {
									layer.msg(sss.msg);
								}

								layer.msg(sss['XM'] + ',欢迎回来!');
								$("#YGXM").val(sss['XM']); //把姓名单独查出来
							}
						});
						//yc(); //调用写异常的方法
					} else {
						layer.msg("请扫工号牌");
					}

					$("#qr").select(); //画面准备完毕后就选择扫码输入框

				});

				$('#mytable').jexcel({
					allowDeleteColumn: false, //不允许删除行
					rowDrag: false, //不允许拖动
					allowManualInsertRow: false, //禁止用户插入一个新列(只能存在1笔)
					allowManualInsertColumn: false, //不允许新增列
					columnSorting: false, //不允许排序
					columns: DuXie, //列的读写控制
					colHeaders: LieMing, //列名的控制
					colWidths: LieKuan //列宽的控制
				});

				//这里是扫描输入框的回车按下后执行的程式
				$("#qr").keypress(function(e) {
					//如果按下的键值是回车键13就执行以下语句
					if(e.keyCode === 13) {
						//扫描输入框选择
						$("#qr").select();
						//定义局部变量QRC=扫描输入框的值
						var QRC = $("#qr").val();
						//取前面两个字符进行下面的判断

						var s2 = QRC.substr(0, 2);

						/*
						TAAK17B2681-2 T2865811K-Q4801A 10 002
						编带站的正则测试
						11 11.7 2.7 2.8 C22 U0C22Y1 80 90 65 BPI31798706019-1 AK187029C-X T2850812K-Q31B3A 8
						http://tool.oschina.net/regex测试通过
						*/
						var patt1 = /^[A-Z]{1,2}[A-Z\d]*\-?[A-Z\d]?(?= T)/; //AK17B2681-2 -> mg组装规格书工单号
						var patt2 = /\d{3}$/; //002组装规格书盒序号
						var patt3 = /[A-Z]{1,2}[A-Z\d]*\-?[A-Z\d]?(?= T)/; //AK187029C-X分光标签工单号
						var patt4 = /BP[A-Z][A-Z\d]*\-?[A-Z\d]*(?= [A-Z]{1,2})/; //BPI31798706019-1分光标签序号(用作唯一码)
						var patt5 = /BP[A-Z]\d{4}/; //机台号或MES机台EQ
						var patt6 = /\d+(?= \d{3}$)/; //组装规格书基准片数
						var patt7 = /^TA/; //兼容,还是要替换掉TA
						var patt8 = /T.*?(?= \d{2} \d{3}$)/; //判断料号 T2865811K-Q4801A
						var patt9 = /EM\d{5}/; //判断工号
						var cpxhstr = QRC.replace(/^TA.*?(?= T)/, ""); //替换掉开头的【TAAK17B2681-2】还留了个空格在头

						if(QRC == "EXC04") { //先独立判断,和下面不影响
							$("#GDH").val("点检工单默认为空");
						} else if(QRC == "EXC22") {
							$("#GDH").val("待机工单默认为空");
						}

						var string1 = QRC.replace(patt7, ""); //取出工单号

						if(patt1.test(string1) || patt3.test(string1)) { //判断是否为工单号
							if(patt1.test(string1)) { //组装规格书工单号测试通过
								GD = patt1.exec(string1) + ""; //取出纯工单号
								//prqrf(); //调用pr确认的function
								var string2 = patt1.exec(string1) + "-" + patt2.exec(string1); //取出工单号盒号
								GDH8HH = string2; //这里直接拿现成的
								var GongDanHao = patt1.exec(string1); //取出纯工单号
								hehao = patt2.exec(string1); //取出盒号
								liaohao = patt8.exec(cpxhstr); //取出料号

								var xxx = QRC;
								$("#QRC").val(xxx);

								$("#liaohao").val(liaohao[0]); //写料号
								$("#GDH").val(GongDanHao); //写工单号
								$("#hehao").val(hehao[0]); //写盒号

								JTGD[$("#DQJT").val()] = string2; //机台工单索引机台=工单号盒号
								if($("#DQJT").val() !== "") { //如果当前机台不为空,就调用zggs
									console.log("请开始你的表演");
									zzggs();
								} else {
									layer.msg('机台号不能为空');
								}

							} else if(patt3.test(QRC)) { //否则它是分光标签
								var string2 = patt3.exec(QRC) + "-" + patt4.exec(QRC); //取出分光标签的工单号
								$("#GDH").val(string2); //写分光标签的工单号
								JTGD[$("#DQJT").val()] = string2; //写当前机台的工单号
							} else {
								layer.msg('工单号的正则不通过，请检查！<br/>组装规格书是否是TA开头？<br/>编带站卷盘标签是否包含工单号');
							}
						} else if(patt5.test(QRC)) { //取到工单号就不会跑来取机台号了吧,编带机上面的流水码是BPI分光机自动标签s2==="BP" && $("#GDH").val() !=="")
							var dqjt = patt5.exec(QRC); //当前机台

							var string3 = ""; //前三码文本

							string3 = dqjt[0].substr(0, 3); //文本3等于当前机台的前三码(待测)

							$("#DQJT").val(dqjt[0]); //写当前机台
							JTGD[dqjt[0]] = $("#GDH").val(); //当前机台的工单号

						} else if(STR2[s2]) {
							$("#" + STR2[s2]).val(QRC); //如果文本2等于

							if(s2 === "EM") { //判断如果是工号就写WEB存储
								localStorage.scyggh = QRC;
								location.reload(); //如果是工号,还要清除字典内容,重新来过...所以干脆刷新一下好了,下面是语句
							}
						} else {
							layer.msg('员工工号、当前机台和工单号不允许为空');
						}
					}
				});

			}); //layui尾巴

			Date.prototype.format = function(fmt) { //这里是日期格式化公式
				var o = {
					"M+": this.getMonth() + 1, //月份
					"d+": this.getDate(), //日
					"h+": this.getHours(), //小时
					"m+": this.getMinutes(), //分
					"s+": this.getSeconds(), //秒
					"q+": Math.floor((this.getMonth() + 3) / 3), //季度
					"S": this.getMilliseconds() //毫秒
				}

				if(/(y+)/.test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				}

				for(var k in o) {
					if(new RegExp("(" + k + ")").test(fmt)) {
						fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
					}
				}

				return fmt;
			}

			function zzggs() { //显示当前工单号的所有组装规格书列表
				ZuZhuangZD = {};

				Dhtml = "";
				var datajson = "";
				datajson = JSON.stringify({
					'getGongDanHao': $("#GDH").val(),
					'getHeHao': hehao[0],
				});

				$.ajax({
					type: 'POST',
					url: "http://172.31.3.27:84/index.php?s=/scancode/Assemblyspec/zzggs", //172.31.3.27:84//127.0.0.1:85
					data: "zzggjson=" + datajson,
					dataType: "json",
					success: function(sss) {
						if(sss.msg) {
							layer.msg(sss.msg);
						}
						if(sss.CD >= 1) {
							$("#YCQ").html("");

							for(k in sss.ZZGG) {
								var hid = sss['ZZGG'][k]['hid'];
								var box_nub = sss['ZZGG'][k]['box_nub']
								var series = sss['ZZGG'][k]['series'];
								var title = sss['ZZGG'][k]['title'];

								ZuZhuangZDArr[hid] = [hid, box_nub, series, title];

								if(box_nub == hehao[0]) {
									ZuZhuangZD[hid] = hid + '丨盒序号：' + box_nub + '丨规格书名称：' + title + '丨产品系列：' + series;
									html = "<button id='" + hid + "' class='layui-btn layui-btn-warm layui-btn-fluid' data-d='" + hid + "' onclick='showZZGG(this)' style='text-align:left'>" + ZuZhuangZD[hid] + "</button>";
									$("#YCQ").append(html);
								} else {
									ZuZhuangZD[hid] = hid + '丨盒序号：' + box_nub + '丨规格书名称：' + title + '丨产品系列：' + series;
									html = "<button id='" + hid + "' class='layui-btn layui-btn-normal layui-btn-fluid' data-d='" + hid + "' onclick='showZZGG(this)' style='text-align:left'>" + ZuZhuangZD[hid] + "</button>";
									$("#YCQ").append(html);
								}
							}
						}
						tesapi();
					}
				});
			}

			function showZZGG(data) {
				ZuZhuangZD1 = {};
				var d1 = data.getAttribute("data-d");
				data.disabled = true; //开始时间按钮被禁用
				data.className = 'layui-btn  layui-btn-disabled layui-btn-fluid'; //添加禁用渲染
				var tid = data.getAttribute("id");
				tesapi(); //这里有坑
			}

			function showTb(data) {
				var d1 = data.getAttribute("data-d");
				data.className = 'layui-btn layui-btn-warm layui-btn-fluid'; //添加颜色标识当前点击

				var tid = data.getAttribute("id");
				var yggh = $('#YGGH').val();

				$("#did").val(d1);
				data2[d1] = [d1, tid, ZuZhuangZDArr1[d1][1], ZuZhuangZDArr1[d1][2], ZuZhuangZDArr1[d1][3]];

				$('#mytable').jexcel('setData', [
					data2[d1]
				]);

				$("html,body").animate({
					scrollTop: $("#mytable").offset().top
				}, 500);
			}

			$("#keep").click(function() {
				DD = [];
				var Ddata = $('#mytable').jexcel('getData');

				//判断是否为空，为空不让保存
				if($("#GDH").val() && $("#DQJT").val() && Ddata[0][0] != "") {
					var gongdanhao = $("#GDH").val() + "-" + $("#hehao").val();
					for(var k in Ddata) {
						DD.push({
							"did": Ddata[k][0],
							"tid": Ddata[k][1],
							"qrc": $("#QRC").val(),
							"equip": $("#DQJT").val(), //设备编号
							"gdhhh": gongdanhao,
							'work_no': $("#GDH").val(), //工单号
							'box_nub': $("#hehao").val(), //盒号
							"operator": $("#YGGH").val(), //作业员
							"box_id": Ddata[k][4], //真正的料盒编号
							"p_qty": Ddata[k][5], //生产数
							"t_qty": Ddata[k][6], //转序数
							"b_qty": Ddata[k][7], //不良数
							"comment": Ddata[k][8] //备注
						});
					}
					//
					$.ajax({
						type: 'POST',
						url: "http://172.31.3.27:84/index.php?s=/scancode/Assemblyspec/keep", //172.31.3.27:84//127.0.0.1:85
						data: "keepjson=" + JSON.stringify({
							'DD': DD
						}),

						dataType: "json",
						success: function(sss) {
							console.log(sss);
							if(sss['msg']) {
								layer.msg(sss['msg']);
								if(sss['msg'] == '保存成功') {
									$('#mytable').jexcel('setData', [
										[]
									]);

								}
							}
						}
					});
				} else {
					layer.msg("机台号、工单号为空或未选择工序时不能保存！");
				}

			});

			function tesapi() {
				ZuZhuangZD1 = {};
				var datajson = "";
				datajson = JSON.stringify({
					'getJiTai': $("#DQJT").val(), //机台号
					'getGongDanHao': GDH8HH, //工单号
					'getLiaoHao': liaohao[0], //料号
					'getYGGH': $("#YGGH").val() //员工工号
				});

				$.ajax({
					type: 'POST',
					url: "http://172.31.3.27:84/index.php?s=/scancode/Assemblyspec/zzggxq", //172.31.3.27:84//127.0.0.1:85
					data: "zzggxqjson=" + datajson,
					dataType: "json",
					success: function(sss) {
						var testid = 0;
						$("#GGXQ").html("");
						for(k in sss.ZZGGXQ) {
							var zzz = sss['ZZGGXQ'][k]['tid']; //交易档id
							var did = sss['ZZGGXQ'][k]['did']; //基础档身id
							var seq = sss['ZZGGXQ'][k]['seq']; //工序序号
							var workseq = sss['ZZGGXQ'][k]['workseq']; //工序名
							var techComment = sss['ZZGGXQ'][k]['techComment']; //工艺说明
							var JTH = sss['ZZGGXQ'][k]['equip']; //机台号
							var lhbh = sss['ZZGGXQ'][k]['box_id']; //料盒唯一编号
							var zyy = sss['ZZGGXQ'][k]['operator']; //作业员
							ZuZhuangZDArr1[did] = [zzz, workseq, techComment, lhbh]; //组装规格书字典,给其它按钮function使用

							if(zyy) { //如果有作业员,就认为是已经被报工了
								ZuZhuangZD1[did] = '序号：' + seq + '丨工序：' + workseq + '丨机台号：' + JTH + '丨料盒唯一编号：' + lhbh + '丨作业员：' + zyy;
								html = "<button style='text-align:left' id='" + zzz + "' class='layui-btn layui-btn-disabled layui-btn-fluid' data-d='" + did + "'onclick='showTb(this)'>" + ZuZhuangZD1[did] + "</button>";
								$("#GGXQ").append(html);
								$("#" + zzz).attr('disabled', true);
							} else {
								ZuZhuangZD1[did] = '序号：' + seq + '丨工序：' + workseq + '丨工艺说明：' + techComment;
								html = "<button style='text-align:left' id='" + zzz + "' class='layui-btn layui-btn-fluid' data-d='" + did + "'onclick='showTb(this)'>" + ZuZhuangZD1[did] + "</button>";
								$("#GGXQ").append(html);
							}
						}
					}
				});
			}

			$("#qr").click(function() {
				$("#qr").select();
			});
		</script>
	</body>

</html>